{"componentChunkName":"component---src-templates-post-jsx","path":"/[Algorithm]최장증가수열(LIS)/","result":{"data":{"site":{"siteMetadata":{"title":"SUbbb"}},"markdownRemark":{"id":"8aa61523-9a0a-5cc7-a670-9750d851b450","excerpt":"🧷 최장 증가 수열 가장 긴 증가하는 부분 수열(Longest Increasing Sequence)로, 일반적으로는 DP를 이용해 해결한다. 🪚 DP를 활용한 구현 및 시간 복잡도 하지만 위와 같이 DP를 활용하는 방식의 시간 복잡도는 이다. 이를 줄이는 좋은 방법은 이분 탐색을 활용하는 것이다. 🪚 이분 탐색을 활용한 구현 및 시간 복잡도 LIS를 구성…","html":"<h2>🧷 최장 증가 수열</h2>\n<p>가장 긴 증가하는 부분 수열(Longest Increasing Sequence)로, 일반적으로는 DP를 이용해 해결한다.</p>\n<deckgo-highlight-code  terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">{7, 2, 3, 8, 4, 5} -&gt; LIS -&gt; {2, 3, 4, 5}</code>\n        </deckgo-highlight-code>\n<h3>🪚 DP를 활용한 구현 및 시간 복잡도</h3>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">int[] arr = {7, 2, 3, 8, 4, 5};\nint[] dp = new int[arr.length]; // LIS 저장 배열\nint max = 0;\n\nfor (int i = 1; i &lt; dp.length; i++) {\n  for (int j = i - 1; j &gt;= 0; j--) {\n    if (arr[i] &gt; arr[j]) {\n      dp[i] = (dp[i] &lt; dp[j] + 1) ? dp[j] + 1 : dp[i];\n      if (max &lt; dp[i]) max = dp[i];\n    }\n  }\n}\n\nint[] answer = new int[max + 1];\nint check = dp[dp.length - 1];\nfor (int i = dp.length - 1; i &gt;= 0; i--) {\n  if (dp[i] == check) {\n    answer[check] = arr[i];\n    check--;\n  }\n}\n\n// DP에 저장된 값 중 최댓값 + 1이 LIS의 길이\nSystem.out.println(Arrays.toString(answer));</code>\n        </deckgo-highlight-code>\n<blockquote>\n<p>하지만 위와 같이 <strong>DP를 활용하는 방식의 시간 복잡도</strong>는 <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><msup><mi>N</mi><mn>2</mn></msup><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N^2)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.064108em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141079999999999em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span></span>이다.</p>\n</blockquote>\n<p>이를 줄이는 좋은 방법은 <strong>이분 탐색</strong>을 활용하는 것이다.</p>\n<h3>🪚 이분 탐색을 활용한 구현 및 시간 복잡도</h3>\n<p>LIS를 구성할 때 이분 탐색을 활용한다. </p>\n<ul>\n<li>LIS의 형태를 유지하기 위해 주어진 배열의 인덱스를 하나씩 살펴본다.</li>\n<li>해당 숫자가 들어갈 위치를 이분 탐색으로 탐색해 삽입한다.</li>\n</ul>\n<blockquote>\n<p>일반적으로 이분 탐색의 시간 복잡도는 <span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>l</mi><mi>o</mi><mi>g</mi><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(logN)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.01968em;\">l</span><span class=\"mord mathnormal\">o</span><span class=\"mord mathnormal\" style=\"margin-right:0.03588em;\">g</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mclose\">)</span></span></span></span>이다. 따라서 이를 이용해 시간 복잡도를 <strong><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mi>l</mi><mi>o</mi><mi>g</mi><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(NlogN)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mord mathnormal\" style=\"margin-right:0.01968em;\">l</span><span class=\"mord mathnormal\">o</span><span class=\"mord mathnormal\" style=\"margin-right:0.03588em;\">g</span><span class=\"mord mathnormal\" style=\"margin-right:0.10903em;\">N</span><span class=\"mclose\">)</span></span></span></span></strong> 으로 줄일 수 있다.</p>\n</blockquote>\n<img src='https://i.imgur.com/tPAmqre.png' width='80%'>\n<deckgo-highlight-code language=\"java\" terminal=\"carbon\" theme=\"a11y-dark\" line-numbers=\"true\"  >\n          <code slot=\"code\">static int[] arr = {7, 2, 3, 8, 4, 5};\nstatic ArrayList&lt;Integer&gt; lis = new ArrayList&lt;&gt;();\n\npublic static void main(String[] args) {\n  lis.add(arr[0]);\n  int idx = 1;\n  int j = 0;\n\n  while(idx &lt; arr.length) {\n    if (lis.get(j) &lt; arr[idx]) {\n      lis.add(arr[idx]);\n      j++;\n    } else {\n      lis.remove(j);\n      lis.add(binarySearch(j, arr[idx]), arr[idx]);\n    }\n    idx++;\n  }\n\n  System.out.println(j + 1);\n  System.out.println(lis);\n}\n\nprivate static int binarySearch(int right, int target) {\n  // 항상 left는 시작점\n  int left = 0;\n  int mid;\n\n  while(left &lt; right) {\n    mid = (left + right) / 2;\n\n    if (lis.get(left) &lt; target) left = mid + 1;\n    else right = mid;\n  }\n\n  return right;\n}</code>\n        </deckgo-highlight-code>\n<hr>\n<h2>📕 참고</h2>\n<ul>\n<li><a href=\"https://gyoogle.dev/blog\">Tech Interview for developer</a></li>\n<li><a href=\"https://chanhuiseok.github.io/posts/algo-49/\">알고리즘 - 최장 증가 부분 수열(LIS) 알고리즘</a></li>\n</ul>","frontmatter":{"title":"💡 최장 증가 수열(LIS)","date":"May 09, 2022","update":"May 09, 2022","tags":["개발상식","Java","LIS"],"series":"💡 Algorithm"},"fields":{"slug":"/[Algorithm]최장증가수열(LIS)/","readingTime":{"minutes":2.555}}},"seriesList":{"edges":[{"node":{"id":"b0e662c4-8dab-58b1-8e34-76fd1f26ad52","fields":{"slug":"/[Algorithm]graph/"},"frontmatter":{"title":"💡 Graph 이론"}}},{"node":{"id":"c1c6944f-734a-5276-8604-a897181207ae","fields":{"slug":"/[Algorithm]문제해결개관/"},"frontmatter":{"title":"💡 문제해결개관"}}},{"node":{"id":"b783c57f-f958-5c14-9935-06b16995e8f1","fields":{"slug":"/[Algorithm]알고리즘분석/"},"frontmatter":{"title":"💡 알고리즘 분석"}}},{"node":{"id":"f3c7f32a-2ead-5bd5-a198-ee5342823217","fields":{"slug":"/[Algorithm]Sorting 알고리즘/"},"frontmatter":{"title":"💡 Sorting 알고리즘"}}},{"node":{"id":"07844ce0-f154-5ad8-b4cc-c6e9bebecc21","fields":{"slug":"/[Algorithm]이분 탐색/"},"frontmatter":{"title":"💡 이분 탐색(Binary Search)"}}},{"node":{"id":"66ab5e60-ccc9-5f85-ad77-8457be4014db","fields":{"slug":"/[Algorithm]HashTable/"},"frontmatter":{"title":"💡 해시 테이블 (Hash Table)"}}},{"node":{"id":"8aa61523-9a0a-5cc7-a670-9750d851b450","fields":{"slug":"/[Algorithm]최장증가수열(LIS)/"},"frontmatter":{"title":"💡 최장 증가 수열(LIS)"}}},{"node":{"id":"db216133-c9cd-5948-babf-f437eb127d7c","fields":{"slug":"/[Algorithm]DynamicProgramming/"},"frontmatter":{"title":"💡 DP (Dynamic Programming)"}}},{"node":{"id":"fa96dd6e-75c9-5a7c-887c-1993125cc7f0","fields":{"slug":"/[Algorithm]최소공통조상(LCA)/"},"frontmatter":{"title":"💡 최소 공통 조상(LCA)"}}},{"node":{"id":"268a7d15-795c-5e9f-9770-eeda9132721d","fields":{"slug":"/[Algorithm]다익스트라알고리즘/"},"frontmatter":{"title":"💡 다익스트라(Dijkstra) 알고리즘"}}},{"node":{"id":"f3a9bade-22f4-54b0-ad9a-b642d6f2b754","fields":{"slug":"/[Algorithm]비트마스크/"},"frontmatter":{"title":"💡 비트마스크(BitMask)"}}}]},"previous":{"fields":{"slug":"/[Programmers]81305/"},"frontmatter":{"title":"👩‍💻 81305. 시험장 나누기"}},"next":{"fields":{"slug":"/[Programmers]64062/"},"frontmatter":{"title":"👩‍💻 64062. 징검다리 건너기"}}},"pageContext":{"id":"8aa61523-9a0a-5cc7-a670-9750d851b450","series":"💡 Algorithm","previousPostId":"04791ec0-62b9-5927-879a-1f0bc5e9fded","nextPostId":"0e5554d3-af02-5a2e-a7cc-df161a1f9258"}},"staticQueryHashes":[]}